package com.szholly.utils.session;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

import com.szholly.utils.session.provider.ISessionProvider;

/**
 * 定义处理Session缓存的Session监听器
 */
public class CachedSessionListener implements HttpSessionListener {

	private Logger logger = Logger.getLogger(CachedSessionListener.class);
	
	@Override
	public void sessionCreated(HttpSessionEvent sessionEvent) {
		logger.info("【sessionCreated】 创建Session："+sessionEvent.getSession().getId());
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
		String url = sessionEvent.getSession().getAttribute("preUrl")+"";
		System.out.println("CachedSessionListener,preUrl--->" + url);
		ISessionProvider httpSession = SessionFactory.getSession();
		
		String userId = (String)httpSession.getObject(ISessionProvider.UserID);
		CurrentSessionHelper.removeUser(userId);
		
		httpSession.clear(sessionEvent.getSession());
		logger.info("【sessionCreated】 注销Session："+sessionEvent.getSession().getId());
	}
}
